package cn.com.fssg.platform.base.handler.impl.gsl.lz;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 工商联履职复制接口
 */
@Component
public class GslLzCopyCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String historyId = jsonObject.getString("id");
        //获取之前数据的考核项
        String selectSql = "select * from gsl_assessment_subject where assessment_id=:id and del_flag='0'";
        Map<String, Object> selectParam = new HashMap<>();
        selectParam.put("id", historyId);
        List<Map<String, Object>> assList = jdbcTemplate.queryForList(selectSql, selectParam);

        Map<String, Object> param = JSONObject.parseObject(jsonObject.toJSONString());
        String newId = BizUtil.cur().getSnowflakeIdStr();
        param.put("id", newId);
        param.put("assessment_name", jsonObject.get("assessment_name") + "_副本");
        param.put("update_by", BizUtil.cur().curUserId());
        param.put("update_date", new Date());
        param.put("create_by", BizUtil.cur().curUserId());
        param.put("create_date", new Date());
        param.put("del_flag", "0");
        param.put("state", "-1");
        param.put("count", "0");
        insertDatabase(param, "gsl_assessment");
        saveAccessmentDetail(assList, newId);
        return null;
    }

    public int saveAccessmentDetail(List<Map<String, Object>> assList, String mainId) {
        AtomicInteger count = new AtomicInteger();
        assList.forEach(item -> {
            Map<String, Object> insertMap = new HashMap<>();
            insertMap.put("id", BizUtil.cur().getSnowflakeIdStr());
            insertMap.put("assessment_id", mainId);
            insertMap.put("subject_name", item.get("subject_name") + "_副本");
            insertMap.put("subject_tips", item.get("subject_tips"));
            insertMap.put("subject_fraction", item.get("subject_fraction"));
            insertMap.put("repeat_count", item.get("repeat_count"));
            insertMap.put("sort", item.get("sort"));

            insertMap.put("update_by", BizUtil.cur().curUserId());
            insertMap.put("create_by", BizUtil.cur().curUserId());
            insertMap.put("update_date", new Date());
            insertMap.put("create_date", new Date());
            insertMap.put("role_scope", "1");
            insertMap.put("parent_role_scope", "1");
            insertMap.put("audit_state", "0");
            insertMap.put("del_flag", "0");
            count.set(insertDatabase(insertMap, "gsl_assessment_subject"));
        });
        return count.get();
    }

    public int insertDatabase(Map<String, Object> param, String tableName) {
        List<String> fields = new ArrayList<>();
        List<String> placeholders = new ArrayList<>();
        param.forEach((k, v) -> {
            if (v != null) {
                fields.add(k);
                placeholders.add(":" + k);
            }
        });

        String sql = String.format("INSERT INTO %s (%s) VALUES (%s)",
                tableName,
                String.join(", ", fields),
                String.join(", ", placeholders)
        );
        return jdbcTemplate.update(sql, param);
    }
}